package com.bianxianmao.myinfo.view;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.bianxianmao.R;
import com.bianxianmao.utils.KeyboardUtils;
import com.bianxianmao.utils.TypeConvertUtils;


/**
 * 充值页面中选择价格view
 */

public class SelectAmountView extends LinearLayout {


    private RadioGroup rgmoney1;
    private RadioGroup rgmoney2;
    private RadioButton rb_20;
    private RadioButton rb_50;
    private RadioButton rb_100;
    private RadioButton rb_200;
    private RadioButton rb_500;
    private RadioButton rb_1000;
    private RadioButton rb_2000;
    private EditText et_sum;

    private Context mContext;

    public SelectAmountView(Context context) {
        super(context);
        initView(context);
        setView();
    }

    public SelectAmountView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        setView();
    }

    public SelectAmountView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        setView();
    }

    /**
     * 初始化
     */
    private void initView(Context context) {
        mContext = context;
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = mInflater.inflate(R.layout.widget_select_amount, null, false);
        rgmoney1 = (RadioGroup) view.findViewById(R.id.rg_money1);
        rgmoney2 = (RadioGroup) view.findViewById(R.id.rg_money2);
        rb_20 = (RadioButton) view.findViewById(R.id.rb_20);
        rb_50 = (RadioButton) view.findViewById(R.id.rb_50);
        rb_100 = (RadioButton) view.findViewById(R.id.rb_100);
        rb_200 = (RadioButton) view.findViewById(R.id.rb_200);
        rb_500 = (RadioButton) view.findViewById(R.id.rb_500);
        rb_1000 = (RadioButton) view.findViewById(R.id.rb_1000);
        rb_2000 = (RadioButton) view.findViewById(R.id.rb_2000);
        et_sum = (EditText) view.findViewById(R.id.et_sum);
        addView(view);
    }

    /**
     * 设置
     */
    private void setView() {
        //默认将第一行第一个选中
        final RadioButton radioButton1 = (RadioButton) rgmoney1.getChildAt(0);
        radioButton1.setChecked(true);

        //默认输入框设置光标不可见
        et_sum.setCursorVisible(false);
        //使EditText默认不弹出软键盘
        KeyboardUtils.hideSoftInput2((Activity) mContext);

        //设置第一行单选按钮监听
        rgmoney1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (radioGroup.getCheckedRadioButtonId()) {
                    case R.id.rb_20:
                        if (rb_20.isChecked()) {
                            clearCheck(0);
                        }
                        break;
                    case R.id.rb_50:
                        if (rb_50.isChecked()) {
                            clearCheck(0);
                        }
                        break;
                    case R.id.rb_100:
                        if (rb_100.isChecked()) {
                            clearCheck(0);
                        }
                        break;
                    case R.id.rb_200:
                        if (rb_200.isChecked()) {
                            clearCheck(0);
                        }
                        break;
                    default:
                        break;
                }
            }
        });

        //设置第二行单选按钮监听
        rgmoney2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (radioGroup.getCheckedRadioButtonId()) {
                    case R.id.rb_500:
                        if (rb_500.isChecked()) {
                            clearCheck(1);
                        }
                        break;
                    case R.id.rb_1000:
                        if (rb_1000.isChecked()) {
                            clearCheck(1);
                        }
                        break;
                    case R.id.rb_2000:
                        if (rb_2000.isChecked()) {
                            clearCheck(1);
                        }
                        break;
                    default:
                        break;
                }
            }
        });

        //设置输入框点击监听
        et_sum.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                //设置光标可见
                et_sum.setCursorVisible(true);
                //设置别选中背景
                et_sum.setBackgroundResource(R.drawable.et_bgselect);
                //清空其他被选中
                rgmoney1.clearCheck();
                rgmoney2.clearCheck();
            }
        });
    }

    /**
     * 清空其他被选中
     *
     * @param flag
     */
    private void clearCheck(int flag) {
        if (flag == 0) {
            rgmoney2.clearCheck();
        } else {
            rgmoney1.clearCheck();
        }
        et_sum.setCursorVisible(false);
        et_sum.setBackgroundResource(R.drawable.et_bgunselect);
        KeyboardUtils.closeKeyboard(mContext, et_sum);
    }

    /**
     * 获取选中的价格
     */
    public int getSelectedAmount() {
        if (rb_20.isChecked()) {
            return 20;
        }
        if (rb_50.isChecked()) {
            return 50;
        }
        if (rb_100.isChecked()) {
            return 100;
        }
        if (rb_200.isChecked()) {
            return 200;
        }
        if (rb_500.isChecked()) {
            return 500;
        }
        if (rb_1000.isChecked()) {
            return 1000;
        }
        if (rb_2000.isChecked()) {
            return 2000;
        }
        //选中的金额
        String selectedAmount = et_sum.getText().toString();
        if (!TextUtils.isEmpty(selectedAmount)) {
            return TypeConvertUtils.String2Int(selectedAmount);
        }
        return 0;
    }
}
